import jieba
from _redis.openRedis import openR
from xpinyin import Pinyin 
from config import cfg
import re


class index :
    def __init__(self) :
        self.j = jieba
        self.j.load_userdict("dict.txt")
     
    def mode_all(self,text,mode = False) :
        text = ''.join(re.findall(u'[\u4e00-\u9fa5]',text))
        text_new = self.j.lcut(text,cut_all = mode)
        return text_new
    
    def mode_search(self,text) :
        text = ''.join(re.findall(u'[\u4e00-\u9fa5]',text))
        text_new = self.j.cut_for_search(text)
        return text_new

class inertRedis :
    def __init__(self):
        self.redis = openR()
        self.p = Pinyin()
        self.n = cfg.redis_hx_name
    
    def stats_count(self,arr):
            for key in arr :
                k = self.p.get_pinyin(key,'')
                n = self.n
                if self.redis.operat_redis_hexists(n=n,k=k) :
                    count = int(self.redis.operat_redis_hget(n=n,k=k)) + 1
                    self.redis.operat_redis_hset(n=n,k=k,v=count)
                else :
                    self.redis.operat_redis_hset(n=n,k=k,v=1)

class toolJson :
    def __init__(self):
        self.node = cfg.node

    def indexJson(self,json):
        arr = []
        for item in json :
            values = json[''+item+'']
            jsType = str(type(values))
            if(item == self.node) :
                arr.append(str(type(values)))
            if(jsType == "<class 'list'>") :
                for nodeT in values :
                    for node in nodeT :
                        if(node == self.node) :
                            arr.append(nodeT[''+node+''])
        return arr
                                